import { isSelectMealFirst } from '@/service/choose-info.js';

// 流程配置
const flowConfig = {
  // 先选机构再选套餐
  1: {
    path: '/choose/card',
    actions: [
      {
        // '确认体检人信息',
        path: '/choose/examiner/confirm',
        type: 'confirm',
        actions: [
          {
            type: 'confirm',
            // '选择机构',
            path: '/choose/hospital',
            actions: [
              {
                type: 'confirm',
                // '选择套餐',
                path: '/choose/meal',
                actions: [
                  {
                    type: 'confirm',
                    // '选择日期',
                    path: '/choose/date',
                    actions: [
                      {
                        type: 'confirm',
                        // '确认订单',
                        path: '/order/confirm',
                      },
                    ],
                  },
                ],
              },
            ],
          },
        ],
      },
    ],
  },
  // 先选套餐再选机构
  2: {
    path: '/choose/card',
    actions: [
      {
        // '选择套餐',
        path: '/choose/city',
        type: 'confirm',
        actions: [
          {
            // '选择城市',
            type: 'confirm',
            path: '/choose/meal',
            actions: [
              {
                type: 'confirm',
                path: '/choose/meal/detail',
                actions: [
                  {
                    type: 'confirm',
                    // '选择机构',
                    path: '/choose/hospital',
                    actions: [
                      {
                        type: 'confirm',
                        // '选择日期',
                        path: '/choose/date',
                        actions: [
                          {
                            type: 'confirm',
                            // '确认体检人',
                            path: '/choose/examiner/form',
                            actions: [
                              {
                                type: 'confirm',
                                // '确认订单',
                                path: '/order/confirm',
                              },
                            ],
                          },
                          {
                            type: 'select',
                            // '选择体检人',
                            path: '/choose/examiner',
                            actions: [
                              {
                                type: 'confirm',
                                // '确认订单',
                                path: '/order/confirm',
                              },
                            ],
                          },
                        ],
                      },
                    ],
                  },
                ],
              },
            ],
          },
        ],
      },
    ],
  },
};

function getActions(node, path) {
  if (node.path === path) {
    return node.actions ? node.actions : [];
  }
  if (!node.actions) return [];

  let len = node.actions.length;
  while (len--) {
    const actions = getActions(node.actions[len], path);
    if (actions) return actions;
  }
}

/**
 * 获取下一个流程页面地址
 * @param {string} currPath  当前页面地址
 * @param {string} actionType 操作类型(用于处理不同的分支情况)
 * @example getNextPage('/hospital'); => '/choose/meal'
 * @returns 返回页面地址(可能为空)
 */
export const getNextPage = (currPath, actionType = 'confirm') => {
  const flowType = isSelectMealFirst() ? 2 : 1;
  const data = flowConfig[flowType];
  const actions = getActions(data, currPath);
  const node = actions.filter((v) => v.type === actionType)[0];
  return node ? node.path : '';
};
